#!/usr/bin/env bash

# Check for trailing whitespace

# Exit on first failure.
set -e

# Exit on unset variable.
set -u

# Change directory to repository root.
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
readonly SCRIPT_DIR
cd "${SCRIPT_DIR}/.."

while read -r line; do
  if grep \
    "\s$" \
    --line-number \
    --with-filename \
    --binary-files=without-match \
    "${line}"; then
    echo "ERROR: Found trailing whitespace";
    exit 1;
  fi
done < <(git ls-files)
